package com.demo.dune.controller;

import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.demo.dune.base.ListResultVO;
import com.demo.dune.base.ResultCodeEnum;
import com.demo.dune.base.ResultVO;
import com.demo.dune.domain.MemCreateInput;
import com.demo.dune.domain.MemberVO;
import com.demo.dune.domain.OrderSubmitInput;
import com.demo.dune.domain.OrderVO;
import com.demo.dune.service.MemberService;
import com.demo.dune.service.OrderService;
import com.demo.dune.service.strategy.MemberStrategyContext;
import com.demo.dune.service.strategy.MemberTypeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * BookSysController.
 * @author Allen
 * @version 1.0 2022-04-03
 */
@Slf4j
@RestController
@RequestMapping("/bookSys")
public class BookSysController {
    
    @Autowired
    OrderService orderService;
    
    @Autowired
    MemberService memberService;
    
    @Autowired
    MemberStrategyContext memStrategyContext;
	
    /**
     * createMem.
     * @param memInput
     * @return ResultVO
     */
    @PostMapping("/createMem")
    public ResultVO createMem(@Valid @RequestBody MemCreateInput memInput) {
    	try {
    		// check memType whether it exists
			String memType = memInput.getType();
			if (!MemberTypeEnum.isInclude(memType)) {
				return new ResultVO(ResultCodeEnum.DATACHECK_ERROR.getCode(), "type value is not correct");
			}
			// check name whether it exists
			MemberVO memberObj = memberService.findByName(memInput.getName());
			if (memberObj != null) {
				return new ResultVO(ResultCodeEnum.DATACHECK_ERROR.getCode(), "this member is already exits");
			}
			
			memStrategyContext.createMem(memInput);
		} catch (Exception e) {
			log.error("createMem exception", e);
			return new ResultVO(ResultCodeEnum.SERVICE_ERROR);
		}
        return new ResultVO(ResultCodeEnum.SUCCESS_HANDLE);
    }
    
    /**
     * submitOrder.
     * @param orderInput
     * @return ResultVO
     */
    @PostMapping("/submitOrder")
    public ResultVO submitOrder(@Valid @RequestBody OrderSubmitInput orderInput) {
    	try {
    		// check order whether it repeat
    		OrderVO orderObj = orderService.findByOrderCode(orderInput.getOrderCode());
    		if (orderObj != null) {
    			return new ResultVO(ResultCodeEnum.DATACHECK_ERROR.getCode(), "order submit repeat");
    		}
    		// check member whether it exists
    		MemberVO member = memberService.findByCode(orderInput.getMemberCode());
    		if (member == null) {
    			return new ResultVO(ResultCodeEnum.DATACHECK_ERROR.getCode(), "this member is not exits");
    		}
    		
	    	orderService.create(orderInput, member);
    	} catch (Exception e) {
    		log.error("submitOrder exception", e);
			return new ResultVO(ResultCodeEnum.SERVICE_ERROR);
		}
        return new ResultVO(ResultCodeEnum.SUCCESS_HANDLE);
    }
    
    /**
     * queryOrders.
     * @return ListResultVO<OrderVO>
     * @throws Exception
     */
    @GetMapping("/queryOrders")
    public ListResultVO<OrderVO> queryOrders() {
        List<OrderVO> orders = new ArrayList<OrderVO>();
		try {
			orders = orderService.findAll();
		} catch (Exception e) {
			log.error("queryOrders exception", e);
			return new ListResultVO<OrderVO>(ResultCodeEnum.SERVICE_ERROR);
		}
        return new ListResultVO<OrderVO>(orders);
    }

}
